<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class GcController extends Controller
{
    //属性 add
    public function getAdd()
    {	
        //查询所有分类
        // $data = DB::table('cate') -> get();
        // dd($data);
        
        //排序sql语句
        // SELECT id,name,path,CONCAT(path,',',id) AS sortPath FROM mll_cate ORDER BY sortPath ;
        // 按照整型排序
        // SELECT id,name,path,CONCAT(path,',',id) AS sortPath FROM mll_cate ORDER BY CAST('sortPath' AS SIGNED );
        
        $data = DB::table('cate')
            -> select(['id', 'name', 'path', DB::raw('CONCAT(path, ",", id) AS sortPath')])
            ->orderBy('sortPath', 'ASC')
            // -> orderBy(DB::raw('CAST("sortPath" AS SIGNED)'),'ASC')
            -> get();

            //dd($data);

        
        foreach($data as $k => $v)
        {
            $count = substr_count($v -> path, ',');
            $str = str_repeat('|----',$count);
            $data[$k] -> c = substr_count($v -> path, ',');
            $data[$k] -> name = $str.$v -> name;
        }
        
        //dd($data);
       return view('admin.goodsattrs.add',['data'=> $data]);
    	
    } 

    //属性 insert
    public function postInsert(Request $request)
    {

        //用统计的方法  让 同一个类的 同一个属性只能添加一回
        //添加一个 ib 字段
        $dd = DB::table('attr_values') -> lists('ib');
        //将拿出来的 ib  变为一个字符串
        $ddd = implode(',',$dd);
        $data = $request -> except('_token');
        //连接两个字段
        $ib = $data['cate_id'].$data['attr_name'];
        //统计次数
        $count = substr_count($ddd,$ib);
        //拿到data中 来插入数据库
        $data['ib'] = $ib;
        
        

        //dd($data);
        if($count == 0)
        {
            //添加数据
            $res = DB::table('attr_values') -> where('cate_id',$data['cate_id']) -> insert($data);
        }else
        {
            return back();
        }   
        

        //判断
        if($res)
        {
            return redirect('/admin/goodsattrs/index') -> with(['info' => '添加成功']);
        }else
        {
            return back() -> with(['error' => '添加失败']);
        }
    }

    //属性 index
    public function getIndex()
    {
        //查询
        // $data = DB::table('cate') -> get();

        // SELECT c1.*,c2.name AS pname,CONCAT(c1.path,',',c1.id) AS sortPath FROM mll_cate AS c1 LEFT JOIN mll_cate AS c2 ON c1.pid = c2.id ORDER BY sortPath ASC;
        
        $data = DB::table('attr_values') -> get();
           
        return view('admin.goodsattrs.index',['data' => $data]);
    }

    public function getEdit($id)
    {
        //查询当前分类
        $data = DB::table('attr_values')
            -> where('id',$id)
            -> first();
           // dd($data);
         //查询所有分类]

        return view('admin.goodsattrs.edit',['data' => $data]);
    }

    //属性 update
    public function postUpdate(Request $request)
    {

        $data = $request -> except('_token');
        //dd($data);
        
        //更新
        $res = DB::table('attr_values')
            -> where('id', $data['id'])
            -> update($data);

        //判断
        if($res)
        {
            return redirect('/admin/goodsattrs/index') -> with(['info' => '更新成功']);
        }else
        {
            return back() -> with(['error' => '更新失败']);
        }
    }

    //属性 delete
    public function getDelete($id)
    {

        $res = DB::table('attr_values')
            -> where('id', $id)
            -> delete();
        
       if($res)
        {
            return back() -> with(['info' => '删除成功']);
        }else
        {
            return back() -> with(['error' => '删除失败']);
        }
    }

}
